from rest_framework import filters

from apps.location.models import Room, Location


# class AppointmentTabFilter(filters.BaseFilterBackend):
#     def filter_queryset(self, request, queryset, view):
#         query = queryset
#         params = request.query_params
#
#         if 'location' in params:
#             query = query.filter(provider_location=params['location'])
#
#         if 'status' in params:
#             query = query.filter(status=params['status'])
#
#         if 'clients' in params:
#             query = query.filter(clients=params['clients'])
#
#         return query

def appointment_filter_queryset(request, queryset):
    query = queryset
    params = request.query_params

    if 'location' in params:
        query = query.filter(provider_location=params['location'])

    if 'e-location' in params:
        location = Location.objects.filter(id=params['e-location']).first()
        rooms = Room.objects.filter(location=location).values_list('id')
        query = query.filter(booking__room__in=rooms)

    if 'status' in params:
        query = query.filter(status=params['status'])

    if 'clients' in params:
        query = query.filter(clients=params['clients'])

    return query


def appointment_list_filter_queryset(request, queryset):
    query = queryset
    params = request.query_params

    if 'location' in params:
        location = params['location']
        room = list(Room.objects.filter(location__id=location).values('id'))
        query = query.filter(booking_room_in=room)

    if 'status' in params:
        query = query.filter(status=params['status'])

    if 'clients' in params:
        query = query.filter(clients=params['clients'])

    if 'room' in params:
        room = Room.objects.filter(id=params['room']).values('id')

        query = query.filter(booking__room=room)

    return query